package com.example.administrator.zhjt.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.zhjt.R;
import com.example.administrator.zhjt.bean.Bill;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/17 0017.
 */

public class BillHistoryAdapter extends BaseAdapter {

    private List<Bill> object;

    private Context context;
    private LayoutInflater layoutInflater;

    public BillHistoryAdapter(Context context,List<Bill> list){
        this.object=list;
        this.context=context;
        this.layoutInflater=LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return object.size()+1;
    }

    @Override
    public Bill getItem(int position) {
        return object.get(position-1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.item_history,null);
            convertView.setTag(new ViewHolder(convertView));
        }
        if (position!=0) {
            initializeView( getItem(position), position, (ViewHolder) convertView.getTag());
        }
        return convertView;
    }

    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm");
    private void initializeView(Bill object,int position,ViewHolder viewHolder){
        viewHolder.tv_No.setText(position+"");
        viewHolder.tv_CarId.setText(object.getCarId()+"");
        viewHolder.tv_Money.setText(object.getMoney()+"");
        viewHolder.tv_User.setText(object.getUser()+"");
        viewHolder.tv_CreateAt.setText(simpleDateFormat.format(new Date(object.getCreateAt())));
    }


    protected class ViewHolder{
        private TextView tv_No;
        private TextView tv_CarId;
        private TextView tv_Money;
        private TextView tv_User;
        private TextView tv_CreateAt;

        public ViewHolder(View view){
            tv_No= (TextView) view.findViewById(R.id.tv_no);
            tv_CarId= (TextView) view.findViewById(R.id.tv_carid);
            tv_Money= (TextView) view.findViewById(R.id.tv_money);
            tv_User= (TextView) view.findViewById(R.id.tv_user);
            tv_CreateAt= (TextView) view.findViewById(R.id.tv_create_at);

        }

    }

}
